package blogTest;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;

public class Run {

    public static void takeScreenshot(WebDriver driver, String name) throws IOException {
        //截图
        File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dtf2 = new SimpleDateFormat("HH-mm-ss");
        String n = dtf.format(System.currentTimeMillis());
        String s = dtf2.format(System.currentTimeMillis()) + name;
        String fileName = "src/test/images/" + n + "/" + s + ".png";
        File file1 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file1, new File(fileName));
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        // 初始化浏览器
        WebDriverManager.chromedriver().setup();

        // 设置chrome配置 允许访问所有链接
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        //隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));
        LoginTest loginTest = new LoginTest();
        LogonTest logonTest = new LogonTest();
        HomePageTest homePageTest = new HomePageTest();
        BlogEditTest blogEditTest = new BlogEditTest();
        ComposeBlogTest composeBlogTest = new ComposeBlogTest();
        LogOutTest logOutTest = new LogOutTest();

        logonTest.testLogon(driver);
        loginTest.testLogin(driver);
        homePageTest.testHomePage(driver);
        composeBlogTest.testComposeBlog(driver);
        blogEditTest.testBlogEdit(driver);
        logOutTest.testLogOut(driver);

    }
}
